#include "my_header.h"
#include "analysis.h"
#include "transfile.h"
#include "sendMsg.h"
#include "threadPool.h" 
// 拼接最新的文件路径
//判断是否有此目录
int putsFile(const char* finalPath,Canalysis_t* canalysis,int netfd){
    char query[4096];
    char dirpath[1024]={0};
    snprintf(dirpath,strlen(finalPath)-strlen(canalysis->lastname),"%s",finalPath);
    if(strlen(dirpath)==0){
        sprintf(dirpath,"/");
    }
    DEBUG_PRINT("dirpath is %s\n",dirpath);
    sprintf(query,"select fid from file_table where username = '%s' and path = '%s'",canalysis->username,dirpath);
    DEBUG_PRINT("before query %s\n",dirpath);
    // DEBUG_PRINT("%x\n",canalysis->mysql);
    DEBUG_PRINT("mysql= %s\n",query);
    /* pthread_mutex_lock(&pthreadPool->mutex); */
    int qret = mysql_query(canalysis->mysql,query);
    /* pthread_mutex_unlock(&pthreadPool->mutex); */
    DEBUG_PRINT("after auery %s\n",dirpath);
    if(qret != 0){
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
        DEBUG_PRINT("putsFile mysql_query failed\n");
        exit(-1);
    }

    DEBUG_PRINT("dirpath is %s\n",dirpath);
    /* pthread_mutex_lock(&pthreadPool->mutex); */
    MYSQL_RES* result = mysql_store_result(canalysis->mysql);
    DEBUG_PRINT("dirpath is %s\n",dirpath);
    unsigned long rowNumber = mysql_num_rows(result);
    /* pthread_mutex_unlock(&pthreadPool->mutex); */
    if(rowNumber == 0){
        // 说明没找到对应的目录
        send(netfd,"0",1,MSG_NOSIGNAL);
        sprintf(canalysis->result,"目录不存在");
        return 0;
    }
    int fid;//存储目录fid

    DEBUG_PRINT("before send %s\n",dirpath);
    char buf[]="1";
    send(netfd,"1",1,MSG_NOSIGNAL);

    DEBUG_PRINT("after %s\n",dirpath);
    /* pthread_mutex_lock(&pthreadPool->mutex); */
    MYSQL_ROW fid_t=mysql_fetch_row(result);
    /* pthread_mutex_unlock(&pthreadPool->mutex); */
    fid=atoi(fid_t[0]);
    DEBUG_PRINT("dirpath is %d\n",fid);


    //hash判断,进入此步客户端序发送文件hash值
    char sha1str[41] = {0};

    recv(netfd,sha1str,40,MSG_WAITALL);
    // 获取文件对应的哈希值
    // 根据哈希查找有无同名文件
    DEBUG_PRINT("sha1 is %s\n",sha1str);
    long filesize=0;
    bzero(query,sizeof(query));
    sprintf(query,"select filesize from file_table where hash_sha1 = '%s'",sha1str);
    DEBUG_PRINT("before mysql\n");
    DEBUG_PRINT("query=%s\n",query);
    /* pthread_mutex_lock(&pthreadPool->mutex); */
    qret = mysql_query(canalysis->mysql,query);
    /* pthread_mutex_unlock(&pthreadPool->mutex); */
    DEBUG_PRINT("after mysql\n");
    if(qret != 0){
        /* pthread_mutex_lock(&pthreadPool->mutex); */
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
        /* pthread_mutex_unlock(&pthreadPool->mutex); */
        DEBUG_PRINT("putsFile mysql_query failed\n");
        exit(-1);
    }
    /* pthread_mutex_lock(&pthreadPool->mutex); */
    MYSQL_RES* result2 = mysql_store_result(canalysis->mysql);
    rowNumber = mysql_num_rows(result2);
    mysql_free_result(result);
    /* pthread_mutex_unlock(&pthreadPool->mutex); */
    if(rowNumber == 0){
        // 说明没找到对应的文件，需要传上去
        // 传表

        char buf[]="1";
        DEBUG_PRINT("before send 1\n");
        send(netfd,"1",1,MSG_NOSIGNAL);
        DEBUG_PRINT("正常buf%s\n",buf);
        bzero(query,sizeof(query));

        DEBUG_PRINT("absoulatepath is %s\n",canalysis->absolutePath);
        chdir(canalysis->absolutePath);
        DEBUG_PRINT("after chdir\n");
        recvFile(netfd,&filesize,sha1str);
        DEBUG_PRINT("after recvfile,filesize = %ld\n",filesize);
        sprintf(query,"insert into file_table (filename,username,parent_id,hash_sha1,path,type,filesize) \
                values ('%s','%s',%d,'%s','%s','%s',%ld)",canalysis->lastname,canalysis->username,fid,sha1str,finalPath,"f",filesize);
        /* pthread_mutex_lock(&pthreadPool->mutex); */
        int qret = mysql_query(canalysis->mysql,query);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
            DEBUG_PRINT("putsFile mysql_query failed\n");
            exit(-1);
        }
        /* pthread_mutex_unlock(&pthreadPool->mutex); */
        // 传文件
        sprintf(canalysis->result,"文件不存在，puts命令执行完毕");
    }
    else{
        // 已经有这个文件了，快传
        // 只传表

        bzero(query,sizeof(query));
        sprintf(query,"select * from file_table where username = '%s' and path = '%s' ",canalysis->username,finalPath);

        int qret = mysql_query(canalysis->mysql,query);
        MYSQL_RES *rows=mysql_store_result(canalysis->mysql);

        int num=mysql_num_rows(rows);
        if(num !=0){   
            sprintf(canalysis->result,"文件已存在，puts命令执行完毕");
            mysql_free_result(rows);
            return 0;
        }
        mysql_free_result(rows);



        DEBUG_PRINT("before fetch\n");
        /* pthread_mutex_lock(&pthreadPool->mutex); */
        num=mysql_num_rows(result2);
        DEBUG_PRINT("%d\n",num);
        MYSQL_ROW row=mysql_fetch_row(result2);
        /* pthread_mutex_unlock(&pthreadPool->mutex); */
        filesize =atol(row[0]);
        bzero(query,sizeof(query));
        DEBUG_PRINT("before send 0\n");
        send(netfd,"0",1,MSG_NOSIGNAL);
        DEBUG_PRINT("%s\n",canalysis->lastname);
        sprintf(query,"insert into file_table (filename,username,parent_id,hash_sha1,path,type,filesize) \
                values ('%s','%s',%d,'%s','%s','%s',%ld)",canalysis->lastname,canalysis->username,fid,sha1str,finalPath,"f",filesize);
        /* pthread_mutex_lock(&pthreadPool->mutex); */
        qret = mysql_query(canalysis->mysql,query);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
            DEBUG_PRINT("putsFile mysql_query failed\n");
            exit(-1);
        }
        /* pthread_mutex_unlock(&pthreadPool->mutex); */

        sprintf(canalysis->result,"文件已存在，puts命令执行完毕");
    }
    mysql_free_result(result2);
    /* mysql_close(canalysis->mysql); */
    return 0;
}


int getsFile(const char* finalPath,Canalysis_t* canalysis,int netfd)
{   //cd到当前目录

    char request[1024]={0};
    sprintf(request,"select hash_sha1 from file_table where username = '%s' and type = 'f'and path = '%s'",canalysis->username,finalPath);
    int ret=mysql_query(canalysis->mysql,request);
    if(ret != 0)
    {
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(canalysis->mysql));
        DEBUG_PRINT("getsFile mysql_query failed\n");
        exit(-1);
    }
    MYSQL_RES* rows = mysql_store_result(canalysis->mysql);
    unsigned long rowNumber = mysql_num_rows(rows);
    if(rowNumber == 0){
        // 说明没找到对应的目录
        sendMsg(netfd,"0");
        sprintf(canalysis->result,"文件不存在");
        return 0;
    }
    MYSQL_ROW row =mysql_fetch_row(rows);
    sendMsg(netfd,"1");
    bzero(canalysis->result,canalysis->resultLength);
    sendMsg(netfd,canalysis->lastname);
    sprintf(canalysis->result,"%s",row[0]);
    chdir(canalysis->absolutePath);
    sendFile(netfd,canalysis->result);
    return 0;
}


